package com.rocketmq.consumer.utils;

import java.util.concurrent.*;

/**
 * 线程池工具类
 *
 * @author xuziwen
 * @create 2017-08-27 15:39
 */
public class ThreadPollUtil {

    private ThreadPollUtil() {
    }

    public static ExecutorService getExecutorService() {
        return Singleton.INSTANCE.getInstance();
    }

    private enum Singleton {
        INSTANCE;

        private ExecutorService executorService;

        //JVM会保证此方法绝对只调用一次
        Singleton() {
            int corePoolSize = Runtime.getRuntime().availableProcessors() * 2;
            int maximumPoolSize = corePoolSize;
            Long keepAliveTime = 0L;
            TimeUnit unit = TimeUnit.MILLISECONDS;
            BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(corePoolSize * 2);
            RejectedExecutionHandler handler = new ThreadPoolExecutor.CallerRunsPolicy();
            executorService = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
        }

        public ExecutorService getInstance() {
            return executorService;
        }
    }

    public static void main(String[] args) {
        System.out.println();
    }
}
